package com.cyys.modules.system.controller.configure;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyys.common.annotation.SystemLog;
import com.cyys.common.ql.AR;
import com.cyys.common.ql.JSONData;
import com.cyys.common.utils.StringUtil;
import com.cyys.common.validate.Detail;
import com.cyys.common.validate.Edit;
import com.cyys.common.validate.Insert;
import com.cyys.modules.core.controller.BaseAction;
import com.cyys.modules.core.sysParam.SystemConfigureUtil;
import com.cyys.modules.core.sysParam.SystemConstants;
import com.cyys.modules.system.model.configure.Configure;
import com.cyys.modules.system.service.configure.ConfigureService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/sys/configure")
public class ConfigureAction extends BaseAction {

	@Resource
	private ConfigureService configureService;

	@GetMapping("/list")
	@RequiresPermissions("sys:configure:menu")
	public String list() {
		return "module/system/configure/configure-list";
	}

	@GetMapping("/add/input")
	@RequiresPermissions("sys:configure:add")
	public String addInput()  {
		return "module/system/configure/configure-input";
	}

	@ResponseBody
	@PostMapping("/add/save")
	@RequiresPermissions(value = "sys:configure:add")
	@SystemLog(module = "系统设置", methods = "系统设置管理-保存系统设置")
	public AR save(@Validated(Insert.class)Configure configure)  {
		return configureService.save(configure);
	}
	
	@GetMapping("/edit/input")
	@RequiresPermissions("sys:configure:edit")
	public String updateInput(@Validated(Detail.class)Configure configure, Model model)  {
		if (configure != null && configure.getId() != null) {
			model.addAttribute("configure", configureService.getById(configure.getId()));
		}
		return "module/system/configure/configure-input";
	}

	@ResponseBody
	@PostMapping("/edit/save")
	@RequiresPermissions(value = "sys:configure:add")
	@SystemLog(module = "系统设置", methods = "系统设置管理-更新系统设置")
	public AR edit(@Validated(Edit.class) Configure configure)  {
		return configureService.update(configure);
	}
	
	@ResponseBody
	@PostMapping("/list/listInfo")
	@RequiresPermissions("sys:configure:list")
	@SystemLog(module = "系统设置", methods = "系统设置-查询系统设置")
	public String listInfo(Configure configure, Page page)  {
		QueryWrapper queryWrapper = new QueryWrapper(configure);
		queryWrapper.orderByAsc("sort");
		return JSONData.toLayerData(configureService.list(page , queryWrapper,null));
	}


	@ResponseBody
	@PostMapping("/del/deleteById")
	@RequiresPermissions("sys:configure:del")
	@SystemLog(module = "系统设置", methods = "系统设置-删除系统设置")
	public AR deleteById(String id) {
		return configureService.deleteById(id);
	}

	@ResponseBody
	@PostMapping("/del/deleteByIds")
	@RequiresPermissions("sys:configure:del")
	@SystemLog(module = "系统设置", methods = "系统设置-批量删除系统设置")
	public AR deleteByIds(String ids)  {
		return configureService.deleteBatchIds(ids);
	}

	@ResponseBody
	@PostMapping("/init")
	@RequiresPermissions("sys:configure:init")
	@SystemLog(module = "系统设置", methods = "系统设置-系统设置初始化")
	public AR init()  {
		SystemConfigureUtil.init(configureService);
		getServletContext().setAttribute(SystemConstants.SYSTEM_NAME, SystemConfigureUtil.get(SystemConstants.SYSTEM_NAME));
		getServletContext().setAttribute(SystemConstants.SYSTEM_PROVINCE_CODE, SystemConfigureUtil.get(SystemConstants.SYSTEM_PROVINCE_CODE));
		getServletContext().setAttribute(SystemConstants.FILE_VERSION, SystemConfigureUtil.get(SystemConstants.FILE_VERSION));
		return AR.ok("设置更新成功！");
	}


	@ResponseBody
	@RequestMapping("/updateSort")
	@RequiresPermissions(value = "sys:configure:edit")
	public AR updateSort(String id,Integer sort)  {
		if(StringUtil.isBlank(id)){
			return AR.failed("缺少参数【id】");
		}
		if(sort == null){
			sort = 0;
		}
		configureService.updateSort(id,sort);
		return AR.ok("排序更新成功");
	}
}
